#include <fileOpt.h>
#include <errno.h>


int foptRead(const char *filename, char *outstr, long outsize)
{
    if(!(filename && outstr && outsize > 0))
    {
        printf("file read param error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "r");
    if(!tmpFd)
    {
        printf("file read open error %s, %s\n", filename, filename);
        return -1;
    }
    fseek(tmpFd, 0, SEEK_END);
    long tmpsize = ftell(tmpFd);
    if(tmpsize > outsize)
    {
        printf("file size bigger than out:%s:%ld>%ld\n", filename, tmpsize, outsize);
        tmpsize = outsize;
    }
    fseek(tmpFd, 0, SEEK_SET);
    tmpsize = fread(outstr, tmpsize, 1, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}
int foptWrite(const char *filename, const char *instr, long insize)
{
    if(!(filename && instr && insize > 0))
    {
        printf("file write param error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "w+");
    if(!tmpFd)
    {
        printf("file write open error %s,%s\n", filename, strerror(errno));
        return -1;
    }
    int tmpsize = fwrite(instr, insize, 1, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}
